// JavaScript Document
function AdminPageBuilder ( ) {
	PageBuilder.call(this);
}

AdminPageBuilder.prototype = new PageBuilder();

AdminPageBuilder.prototype.constructor = AdminPageBuilder;

AdminPageBuilder.prototype.build = function () {
};

$(document).ready(function() {
	setLogin();
	buildPage(new AdminPageBuilder());
	var admin = getCookie("admin");
	if ( admin == "true" ) {
		var icon_label = getDisplayString("label_icon");
		var title_label = getDisplayString("label_title");
		var author_label = getDisplayString("label_author");
		var date_label = getDisplayString("label_date");
		var rating_label = getDisplayString("label_rating");
		var view_label = getDisplayString("label_view");
		var featured_label = getDisplayString("label_featured");
		var delete_label = getDisplayString("label_delete");
		$("#widgetList").jqGrid({
			datatype: "local",
			height: 300,
			autowidth: true,

			colNames:[icon_label,title_label, author_label, date_label, rating_label, view_label, featured_label, delete_label],
			colModel:[
				{name:'icon', index:'icon', width:60},
				{name:'title', index:'title', width:180},
				{name:'author', index:'author', width:180},
				{name:'date', index:'date', width:180},
				{name:'rating', index:'rating', width:60},
				{name:'view', width:60},
				{name:'featured',width:60},
				{name:'delete',width:60}
			],
			multiselect: false,
			caption: "Widget List",
			pager:'#pager'
		});
		getWidgetList();
		// grid resizer
		$(window).resize(function(e) {
			var newPageWidth = $("#page").width();
			$("#widgetList").jqGrid('setGridWidth', newPageWidth);
	
		});
	}

	$("#viewDialog").dialog({
		autoOpen: false,
		modal: true
	});
	
});


function getWidgetList ( ) {
	$.ajax({
		type:"GET",
		url:store_rest_url+"/widgets/search/*:*/0/5000",
		dataType: "text",
		success: buildGrid
	});
}

function buildGrid(jsonString) {
	var obj = $.parseJSON(jsonString);
	var widgets = obj.widgets;
	if ( widgets == null ) {
		// try SearchResults returned by later versions of edukapp
		widgets = obj.SearchResults;
		if ( widgets == null ) {
			// try just getting top level, might just be an array
			widgets = obj;
		}
	}
	if ( $.isArray(widgets) ) {
		$("#widgetList").jqGrid('clearGridData');
		for ( var i = 0; i < widgets.length; i++ ) {
			var widget = widgets[i];
			if ( widget.icon == null ) {
				widget.icon = "img/default_widget_icon.png";
			}
			var iconString = '<img src="'+widget.icon+'" alt="'+widget.name+'" width="16" height="16"/>';
			var viewString = '<a href="#" onclick="viewWidget('+widget.id+')"><img src="img/view_icon.png" width="16" height="16"/></a>';
			var selectIcon;
			if ( widget.featured == 1 ) {
				selectIcon = '<img src="img/checked.png" width="16" height="16"/>';
			}
			else {
				selectIcon = '<img src="img/not_checked.png" width="16" height="16"/>';
			}
			var featuredString = '<a href="#" onclick="toggleFeatured('+widget.id+')">'+selectIcon+'</a>';
			var deleteString = '<a href="#" onclick="deleteWidget('+widget.id+',\''+widget.name+'\')"><img src="img/delete_icon.png" width="16" height="16"/></a>';
			var ownerName;
			if ( widget.owner ) {
				ownerName = widget.owner.accountInfo.realname;
			}
			else {
				ownerName = "Wookie";
			}
			var widgetData = {	icon:iconString,
								title:widget.name,
								author:ownerName,
								date:widget.created,
								rating:widget.averageRating,
								view:viewString,
								featured:featuredString,
								delete:deleteString
							};
			$("#widgetList").jqGrid('addRowData',i+1,widgetData);
		}
	}
}


function deleteWidget (widgetId,widgetName) {
	if (widgetName == "WidgetStore") {
		alert(getDisplayString("widget_delete_prohibited"));
		return;
	}
	if ( !isAdmin() ) {
		alert (getDisplayString("not_admin"));
		return;
	}
	var result = confirm(getDisplayString("delete_widget_text"));
	if ( result ) {
		var u = getUser();
		if ( u == null ) {
			alert(getDisplayString("not_signed_in"));
			return;
		}
		var uri = store_rest_url+"/widgets/delete/"+widgetId;
		$.ajax({
			type:"DELETE",
			url:uri,
			beforeSend: function(xhr) {
				var authString = getBase64Authentication();
				xhr.setRequestHeader("Authorization", "Basic "+authString);
			},
			dataType:"text",
			success:widgetDeleted,
			error:widgetNotDeleted
		});
	}
}

function widgetDeleted(jsonString) {
	var msg = $.parseJSON(jsonString);
	if ( msg.message == "OK" ) {
		
		// set a timeout of half a second to allow solr to index
		//setTimeout(function(){getWidgetList();}, 250);
		getWidgetList();
	}
	else {
		alert (getDisplayString("widget_not_deleted")+": "+msg.message);
	}
}

function widgetNotDeleted(jqXHR, textStatus, errorThrown ) {
	alert (getDisplayString("widget_not_deleted")+": "+jqXHR.responseText);
}

function toggleFeatured ( widgetId ) {
	if ( !isAdmin() ) {
		alert (getDisplayString("not_admin"));
		return;
	}
	
	var uri = store_rest_url+"/widgets/featured/toggle/"+widgetId;
	$.ajax({
		type:"PUT",
		url:uri,
		beforeSend: function(xhr) {
			var authString = getBase64Authentication();
			xhr.setRequestHeader("Authorization", "Basic "+authString);
		},
		dataType: "text",
		success:widgetToggled,
		error:widgetNotToggled
	});
}


function widgetToggled (jsonString) {
	var msg = $.parseJSON(jsonString);
	if ( msg.message == "true" || msg.message == "false") {
		getWidgetList();
	}
	else {
		alert (getDisplayString("widget_featured_not_toggled"));
	}
}

function widgetNotToggled ( jqXHR, textStatus, errorThrown ) {
	alert (getDisplayString("widget_featured_not_toggled"+": "+jqXHR.reponseText));
}



function viewWidget ( widget_id ) {
	$.ajax({
		type:"GET",
		url:store_rest_url+"/widgets/"+widget_id,
		dataType: "text",
		success: showWidget
	});
}


function showWidget ( jsonString ) {
	var w = $.parseJSON(jsonString);
	// get the width from the iframe info
	//var render = w.renderInfo;
	
	$("#viewContents").html(w.renderInfo);
	var wid = $("#viewContents > iframe").width() + 34;
	$("#viewDialog").dialog("option","title", w.widgetProfile.name );
	$("#viewDialog").dialog("option","width", wid );
	$("#viewDialog").dialog("open");
}
